package Work;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;

public class ChangeRecord {

	// 读取数据库文件，生成管理员信息表
	public ArrayList<Record> read(Jdbc db) {
		ArrayList<Record> list = null;
		try {
			list = db.searchRecord();

			System.out.println("读取数据成功");
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 借阅记录
	public void recordsystem(ArrayList<Book> booklist, ArrayList<Reader> readerlist, ArrayList<Librarian> librarianlist,
			ArrayList<Record> list, Jdbc db) {
		Scanner sc = new Scanner(System.in);
		String id, r_id, l_id;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		ForeignKey foreignkey = new ForeignKey();
		int c;
		System.out.println("书籍借阅系统登录成功！");
		String now = df.format(System.currentTimeMillis());
		String now1 = df.format(System.currentTimeMillis());
		String now2 = df.format(System.currentTimeMillis());
		while(true) {
			System.out.println("请输入您想要进行的借阅操作：\n1.借阅书籍\n2.归还书籍\n3.续借书籍\n（输入-1退出系统）");
			c = sc.nextInt();
			if (c == -1) {
				return;
			} else if (c == 1) {
				while (true) {
					while (true) {
						System.out.println("请输入您要借阅的书籍编号\n(请输入书籍完整编号，若编号为1，则输入“00000001”):");
						id = sc.next();
						if (!foreignkey.FindB_ID(booklist, id)) {
							System.out.println("查无此书，请重新输入");
						} else if(!foreignkey.IfZero(booklist, id)) {
							System.out.println("该书库存量不足或已被借出，请换一本！");
						}else
							break;
					}
					while (true) {
						System.out.println("请输入您的编号:");// 优化！！！！！！
						r_id = sc.next();
						if (!foreignkey.FindR_ID(readerlist, r_id)) {
							System.out.println("查无此人，请重新输入");
						} else
							break;
					}
					db.sql("insert into record (b_id, r_id, borrow_time) value (" + id + ", " + r_id + ", '" + now + "')");
					// Calendar.getInstance().getTime() + "')");
					System.out.println("书籍借阅成功！再次借阅请按任意键，退出请输入-1：");
					if (sc.next().equals("-1"))
						break;
				}

			} else if (c == 2) {
				while (true) {
					while (true) {
						System.out.println("请输入您要归还的书籍编号\n(请输入书籍完整编号，若编号为1，则输入“1”):");
						id = sc.next();
						if (!foreignkey.FindB_ID(booklist, id)) {
							System.out.println("查无此书，请重新输入");
						} else
							break;
					}
					while (true) {
						System.out.println("请输入您的编号:");// 优化！！！！！！
						r_id = sc.next();
						if (!foreignkey.FindR_ID(readerlist, r_id)) {
							System.out.println("查无此人，请重新输入");
						} else
							break;
					}
					while (true) {
						System.out.println("请输入帮您确认的管理员的编号:");// 优化！！！！！！
						l_id = sc.next();
						if (!foreignkey.FindL_ID(librarianlist, l_id)) {
							System.out.println("查无此人，请重新输入");
						} else
							break;
					}
					db.sql("update record set return_time = '" + now2 + "' where b_id = " + id + " and r_id =" + r_id);
					db.sql("update record set lib_confirm = " + l_id + " where b_id = " + id + " and r_id =" + r_id);
					System.out.println("书籍归还成功！再次归还请按任意键，退出请按-1");
					if (sc.next().equals("-1"))
						break;
				}

			} else if (c == 3) {
				while (true) {
					while (true) {
						System.out.println("请输入您要续借的书籍编号\n(请输入书籍完整编号，若编号为1，则输入“00000001”):");
						id = sc.next();
						if (!foreignkey.FindB_ID(booklist, id)) {
							System.out.println("查无此书，请重新输入");
						} else
							break;
					}
					while (true) {
						System.out.println("请输入您的编号:");// 优化！！！！！！
						r_id = sc.next();
						if (!foreignkey.FindR_ID(readerlist, r_id)) {
							System.out.println("查无此人，请重新输入");
						} else
							break;
					}
					db.sql("update record set reborrow_time = '" + now2 + "' where b_id = " + id + " and r_id =" + r_id);
					System.out.println("书籍续借成功！再次借阅请按任意键，退出请按-1");
					if (sc.next().equals("-1"))
						break;
				}

			}
		}
		
	}
}
